package master.usefulClosures;

import java.awt.Color;
import java.awt.Graphics;

import master.*;

public class SumClosure extends ComplexNumberClosure{

	public SumClosure(ComplexNumber... complexNumbers){
		super(complexNumbers);
	}
	
	public ComplexNumber function(ComplexNumber... complexNumbers) {
		if(complexNumbers.length == 0) return null;

		ComplexNumber a = complexNumbers[0].createNewInstance();
		for(int i = 1; i < complexNumbers.length ;i++)
			a.add(complexNumbers[i]);
		
		return a;
	}

	public void draw(Graphics g, int x, int y, float multiplier){
		super.draw(g, x, y, multiplier);
		g.setColor(Color.gray);
		if(complexNumbers.length>0)complexNumbers[0].draw(g, x, y, multiplier);
		for(int i = 1 ; i < complexNumbers.length ; i++){
			int xPos = x;
			int yPos = y;
			for(int j = i-1 ; j >= 0 ; j--){
				xPos += (int)(complexNumbers[j].getReal()*multiplier);
				yPos -= (int)(complexNumbers[j].getImaginary()*multiplier);
			}
			
			complexNumbers[i].draw(g, xPos, yPos,multiplier);
		}
		
	}
	
}
